package com.first.nell.info.face

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.first.nell.R
import com.first.nell.base.BaseActivity
import com.first.nell.data.ReqImageUpload
import com.first.nell.data.ReqSaveImage
import com.first.nell.databinding.ActivityFirstNellFaceDetectBinding
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.*
import com.first.nell.info.idcard.IdentityCardModel
import com.first.nell.launch.UserStateViewModel
import java.util.*


@SuppressLint("CustomSplashScreen")
class FaceDetectActivity : BaseActivity() {

    private lateinit var mFirstNellActivityFirstNellBinding: ActivityFirstNellFaceDetectBinding

    private val mIdentityCardModel by lazy {
        ViewModelProvider(this)[IdentityCardModel::class.java]
    }

    private val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        list.add(mIdentityCardModel)
        return list
    }

    override fun onFirstNellBindingFirstNellViewFirstNellTag(): String {
        return PageTagEnum.selfie.name.uppercase()
    }

    override fun onFirstNellBindingFirstNellContentFirstNellView(): View {
        mFirstNellActivityFirstNellBinding = ActivityFirstNellFaceDetectBinding.inflate(layoutInflater)
        return mFirstNellActivityFirstNellBinding.root
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onFirstNellContentFirstNellViewFirstNellBinded() {
        try {
            //上传图片回调
            mIdentityCardModel.onFirstNellUploadFirstNellImageFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    val result = it.getOrNull()
                    if (result != null) {
                        //保存图片url
                        mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront = result.url
                        mFirstNellActivityFirstNellBinding.imageFaceDetect.setImageBitmap(
                            mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellFront
                        )
                        mFirstNellActivityFirstNellBinding.imageFaceDetectPlaceholder.visibility = View.GONE
                        mFirstNellActivityFirstNellBinding.imageFaceDetect.visibility = View.VISIBLE
                        mFirstNellActivityFirstNellBinding.llFaceDetectDeleteBg.visibility = View.VISIBLE
                        mFirstNellActivityFirstNellBinding.submitInfo.mFirstNellViewFirstNellBinding.textSubmitButton.text =
                            getString(R.string.first_nell_face_confirm_submit)
                    } else {
                        mFirstNellActivityFirstNellBinding.imageFaceDetectPlaceholder.visibility = View.VISIBLE
                        mFirstNellActivityFirstNellBinding.imageFaceDetect.visibility = View.GONE
                        mFirstNellActivityFirstNellBinding.llFaceDetectDeleteBg.visibility = View.GONE
                        mFirstNellActivityFirstNellBinding.submitInfo.mFirstNellViewFirstNellBinding.textSubmitButton.text =
                            getString(R.string.first_nell_face_start_take_pic)
                    }
                } else {
                    mFirstNellActivityFirstNellBinding.imageFaceDetectPlaceholder.visibility = View.VISIBLE
                    mFirstNellActivityFirstNellBinding.imageFaceDetect.visibility = View.GONE
                    mFirstNellActivityFirstNellBinding.llFaceDetectDeleteBg.visibility = View.GONE
                    mFirstNellActivityFirstNellBinding.submitInfo.mFirstNellViewFirstNellBinding.textSubmitButton.text =
                        getString(R.string.first_nell_face_start_take_pic)
                }
            }

            //保存图片回调
            mIdentityCardModel.onFirstNellSaveFirstNellImageFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    //获取流程状态
                    mUserStateViewModel.fetchFirstNellUserFirstNellState()
                }
            }

            //获取流程回调
            mUserStateViewModel.onFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.selfie)
                }
            }

            //face
            mFirstNellActivityFirstNellBinding.imageFaceDetectPlaceholder.setOnClickListener {
                mIdentityCardModel.mFirstNellImageType = IdentityCardModel.IMAGE_IDCARD_FRONT
                mFirstNellActivityFirstNellBinding.imageFaceDetect.recordFirstNellClickFirstNellEvent()
                requestExtPermissoins()
            }

            //face
            mFirstNellActivityFirstNellBinding.imageFaceDetect.setOnClickListener {
                mIdentityCardModel.mFirstNellImageType = IdentityCardModel.IMAGE_IDCARD_FRONT
                mFirstNellActivityFirstNellBinding.imageFaceDetect.recordFirstNellClickFirstNellEvent()
                requestExtPermissoins()
            }

            //face delete
            mFirstNellActivityFirstNellBinding.imageFaceDetectDelete.setOnClickListener {
                mIdentityCardModel.mFirstNellImageType = IdentityCardModel.IMAGE_IDCARD_FRONT
                mFirstNellActivityFirstNellBinding.imageFaceDetectDelete.recordFirstNellClickFirstNellEvent()
                mFirstNellActivityFirstNellBinding.llFaceDetectDeleteBg.visibility = View.GONE
                mFirstNellActivityFirstNellBinding.submitInfo.mFirstNellViewFirstNellBinding.textSubmitButton.text =
                    getString(R.string.first_nell_face_start_take_pic)
                mFirstNellActivityFirstNellBinding.imageFaceDetect.setImageBitmap(null)
                mFirstNellActivityFirstNellBinding.imageFaceDetect.setImageResource(R.drawable.first_nell_face_detect)
            }

            //commit
            mFirstNellActivityFirstNellBinding.submitInfo.setOnClickListener {
                mFirstNellActivityFirstNellBinding.submitInfo.recordFirstNellClickFirstNellEvent()

                //判断是否能提交
                var canSubmit = true
                if (mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront.isNotBlank()) {
                    //能提交
                    mIdentityCardModel.saveFirstNellImage(
                        ReqSaveImage(
                            faceImgUrl = mIdentityCardModel.mFirstNellImageFirstNellUrlFirstNellFront,
                            cardType = "FACE"
                        )
                    )
                } else {
                    canSubmit = false
                }

                if (!canSubmit) {
                    //直接拍照
                    requestExtPermissoins()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted(): String {
        val notFillOptions = mutableListOf<String>()

        return notFillOptions.joinToString("#")
    }

    override fun onFirstNellPermissionsFirstNellRequestFirstNellAllFirstNellPass() {
        //监听人脸状态
        FaceDetectManager.startFaceDetecting(this) { isSuccess, filePath, image ->
            if (isSuccess) {
                onHandlerImagePickResult(filePath, image)
            } else {
                //提示重新做
                Toast.makeText(
                    this,
                    "Please select picture first.",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    /** 处理选择/拍照回调 */
    private fun onHandlerImagePickResult(covetPath: String?, bitmap: Bitmap?) {
        if (covetPath?.isNotBlank() == true) {
            mIdentityCardModel.mFirstNellImageFirstNellPathFirstNellFront = covetPath
            mIdentityCardModel.mFirstNellImageFirstNellBitmapFirstNellFront = bitmap

            //上传图片
            mIdentityCardModel.uploadFirstNellImage(
                ReqImageUpload(
                    filePath = covetPath,
                    cardType = mIdentityCardModel.mFirstNellImageType
                )
            )
        } else {
            Toast.makeText(
                this,
                "Please select picture first.",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    /** 请求相机权限 */
    private fun requestExtPermissoins() {
        requestFirstNellSpecFirstNellPermissions(arrayOf(android.Manifest.permission.CAMERA))
    }
}